$package('itsm.system');

itsm.system.regiterCodeList=function(){
	this.opt='';
	return {
		initDataListGrid:function(){
			var params = $.extend({},jqGridParams, {	
				url:'imessage!findPageIMessage.action',
				caption:'站内留言列表',
				colNames:['ID','发送用户','发送用户姓名','消息内容','回复内容','接收用户','接收用户姓名','发送时间','状态','类型value','类型'],
			 	colModel:[
		 	          {name:'imId',align:'center',width:35},
		 	          {name:'sendUser',align:'center'},
		 	          {name:'sendUserName',align:'center'},
		 	          {name:'message',align:'left'},
		 	          {name:'replyMessage',align:'center',hidden:true},
		 	          {name:'receiveUser',align:'center'},
		 	          {name:'receiveUserName',align:'center'},
		 	          {name:'sendDate',align:'center'},
		 	          {name:'status',align:'center',formatter:itsm.system.regiterCodeList.statusForma},
		 	          {name:'mtypeValue',align:'center',hidden:true},
		 	          {name:'mtype',align:'center',formatter:itsm.system.regiterCodeList.gridFormatterMtype}
			 	],
				jsonReader: $.extend(jqGridJsonReader, {id: "imId"}),
				ondblClickRow:function(rowId){itsm.system.regiterCodeList.messageDetails(rowId)},
				sortname:'imId',
				toolbar:[true,"top"],
				sortorder:'nr_sord',
				rowNum:20, 
				rowList:[5,10,15,20],
				pager:'#systemMessageListPager'
			});
			$("#systemMessageListGrid").jqGrid(params);
			$("#systemMessageListGrid").navGrid('#systemMessageListPager',navGridParams);
			//列表操作项
			$("#t_systemMessageListGrid").css(jqGridTopStyles);
			$("#t_systemMessageListGrid").html($('#systemMessageGridToolbar').html());
			//自适应宽度
			setGridWidth("#systemMessageListGrid","regCenter",10);
		},
		gridFormatterMtype:function(cell, opt, data) {
			var _type = data['mtype']; 
			if (_type === 'reply') {
				return '<span style="color:red;">回复</span>';
			}else if(_type === 'Send'){
				return '<span style="color:blue;">接收</span>';
			}
		},
		statusForma:function(cell, opt, data){
			if (cell === 'Unread' || cell === '未读') {
				return '<span style="color:red;">未读</span>';
			}else if(cell === '已读'){
				return '<span style="color:green;">已读</span>';
			}else{
				return cell;
			}
		},
		openReplyWindow:function(){
			resetForm("#systemMessageList_reply form");
			var rowId = $("#systemMessageListGrid").getGridParam('selrow');
			var rowData=$("#systemMessageListGrid").getRowData(rowId);
			if (rowData['sendUser'] ) {
				if ( rowData['mtype'] === 'reply' || rowData['mtype'] === '回复') {
					msgShow('只能对收到的留言进行回复','show'); return;
				}
				$("#systemMessageList_replyUser").val(rowData['sendUser']);
				$("#systemMessageList_replyUser_value").val($("#systemMessageList_replyUser").val());
				$("#systemMessageList_reply_window").window("open");
			}else{
				//msgAlert(i18n['msg_atLeastChooseOneData'],'error');
				msgShow(i18n['msg_atLeastChooseOneData'],'show');
			}
			$.post('imessage!changeStatus.action','imessageDto.imId='+rowData.imId+'&imessageDto.status=已读',function(data){
				$("#systemMessageListGrid").trigger('reloadGrid',[{"page":"1"}]);
			});
			
		},
		replyMessage:function(){
			if ($('#systemMessageList_reply_window form').form('validate')) {
				/*$('#systemMessageList_reply_window form').submit();
				$('#systemMessageList_reply_window').window('close');*/
				var _param = $('#systemMessageList_reply_window form').serialize();
				$.post('imessage!reply.action',_param,function(data){
					msgShow('回复成功','show');
					$('#systemMessageList_reply_window').window('close');
					$("#systemMessageListGrid").trigger('reloadGrid',[{"page":"1"}]);
				});
			}
		},
		messageDetails:function(rowId){
			var rowData = $("#systemMessageListGrid").jqGrid("getRowData", rowId);
			$("#messageInfo_window").window("open");
			$('#sendUser').text(rowData.sendUser);
			$('#sendContent').text(rowData.message);
			$('#sendTime').text(rowData.sendDate);
			if(rowData.status=='<span style="color:red;">未读</span>'){
				$.post('imessage!changeStatus.action','imessageDto.imId='+rowData.imId+'&imessageDto.status=已读',function(data){
					$("#systemMessageListGrid").trigger('reloadGrid',[{"page":"1"}]);
				});
			}
			
		},
		init:function(){
			itsm.system.regiterCodeList.initDataListGrid();
		}
	}
}();
$(document).ready(itsm.system.regiterCodeList.init);